import { ENUM_AUDIO_CLIP } from "../scripts/Enum";
import AudioManager from "../scripts/manager/AudioManager";
import DataManager from "../scripts/manager/DataManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ZxhGameMgr extends cc.Component {

    @property(cc.Node)
    player0:cc.Node;
    @property(cc.Node)
    player1:cc.Node;
    @property(cc.Node)
    cellRoot:cc.Node;
    @property(cc.Node)
    zxhPanel:cc.Node;
    @property(cc.Node)
    winPanel:cc.Node;
    @property(cc.Node)
    DiceBtn:cc.Node;
    @property([cc.SpriteFrame])
    diceSps:cc.SpriteFrame[]=[]
    @property(cc.Node)
    icon_arraw:cc.Node;
    @property(cc.Sprite)
    musicSp:cc.Sprite;
    @property(cc.Sprite)
    soundSp:cc.Sprite;
    @property([cc.SpriteFrame])
    musicSf:cc.SpriteFrame[]=[]
    @property([cc.SpriteFrame])
    soundSf:cc.SpriteFrame[]=[]

    currAxeIndex_boy = 0;
    finallyIndex_boy = 0;

    currAxeIndex_girl = 0;
    finallyIndex_girl = 0;

    currWhoPlary=0;//0 boy,2 girl

    zxhDes_boy=["挑战20个俯卧撑","拥抱一下你附近的女孩","什么都不做","什么都不做","老实交代自己谈过几次恋爱","讲述一遍你人生最糟糕的一次经历","做一个鬼脸","展示一下你的肌肉","面对面看着对方的眼睛5秒","讲述一遍你曾经的一次糟糕的经历","说出你认为什么女生吸引你"];
    zxhDes_girl=["说出你的初恋对象","透露一下你的三围","什么都不做","什么都不做","老实交代自己谈过几次恋爱","讲述一遍你人生最糟糕的一次经历","做一个鬼脸","你最不喜欢男生做什么事情","面对面看着对方的眼睛5秒","讲述一遍你曾经的一次糟糕的经历","说出你认为什么N男生吸引你"];

    protected start(): void {
        cc.tween(this.icon_arraw)
        .to(0.4,{position:cc.v3(-132,170,0)})
        .to(0.4,{position:cc.v3(-162,170,0)})
        .union()
        .repeatForever()
            .start()

    }

    protected onEnable(): void {
        this.gameReStart();
        this.winPanel.active=false;
        this.zxhPanel.active=false;
        this.node.getChildByName("pauseMinPanel").active=false;

    }

    onclickDiceBtn() {        
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.DICE)
        this.DiceBtn.getChildByName("Background").getComponent(cc.Animation).play();
        this.DiceBtn.getComponent(cc.Button).enabled = false;

        setTimeout(() => {
            let randCount = Math.round(Math.random() * 5 + 1);
            console.log(this.currWhoPlary, "谁", randCount, "骰子到");
            this.DiceBtn.getChildByName("Background").getComponent(cc.Sprite).spriteFrame = this.diceSps[randCount - 1];

            if (this.currWhoPlary == 0) {
                this.finallyIndex_boy += randCount;
            }
            else if (this.currWhoPlary == 1) {
                this.finallyIndex_girl += randCount;
            }
            this.playerMove();
        }, 700);

    }

    playerMove() {
        if (this.currWhoPlary == 0) {
            if (this.currAxeIndex_boy >= this.cellRoot.childrenCount - 1) {//胜利
                this.openWinPanel();
                this.DiceBtn.getComponent(cc.Button).enabled = true;

                return;
            }

            if (this.currAxeIndex_boy >= this.finallyIndex_boy) {
                this.openZxhPanel();
                this.DiceBtn.getComponent(cc.Button).enabled = true;

                return;
            }

            let goIndex = this.currAxeIndex_boy + 1;

            cc.tween(this.cellRoot.children[goIndex])
            .to(0.5,{scale:1.1})
            .to(0.2,{scale:1})
            .start()
    
            cc.tween(this.player0)
            .tag(11)
                .to(0.7, { position: cc.v3(this.cellRoot.children[goIndex].x, this.cellRoot.children[goIndex].y, 0) })
                .call(() => {
                cc.Tween.stopAllByTag(11);

                    this.currAxeIndex_boy += 1;
                    this.playerMove();
                })
                .start()
        }
        else if (this.currWhoPlary == 1) {
            if (this.currAxeIndex_girl>=this.cellRoot.childrenCount-1) {//胜利
                this.openWinPanel();
                this.DiceBtn.getComponent(cc.Button).enabled = true;

                return;
            }

            if (this.currAxeIndex_girl>=this.finallyIndex_girl) {
                this.openZxhPanel();
                this.DiceBtn.getComponent(cc.Button).enabled = true;

                return;
            }

            let goIndex = this.currAxeIndex_girl + 1;

            cc.tween(this.cellRoot.children[goIndex])
            .to(0.5,{scale:1.1})
            .to(0.2,{scale:1})
            .start()
    
            cc.tween(this.player1)
                .to(0.7, { position: cc.v3(this.cellRoot.children[goIndex].x, this.cellRoot.children[goIndex].y, 0) })
                .call(() => {
                    cc.Tween.stopAllByTag(11);

                    this.currAxeIndex_girl += 1;
                    this.playerMove();
                })
                .start()
        }

    }

    openZxhPanel() {
        this.zxhPanel.active = true;

        let desStr = "";
        if (this.currWhoPlary == 0) {
            desStr = this.zxhDes_boy[Math.round(Math.random() * (this.zxhDes_boy.length - 1))];

            this.zxhPanel.getChildByName("titleLabel").getComponent(cc.Label).string = "男生做任务";

        }
        else if (this.currWhoPlary == 1) {
            desStr = this.zxhDes_girl[Math.round(Math.random() * (this.zxhDes_boy.length - 1))];

            this.zxhPanel.getChildByName("titleLabel").getComponent(cc.Label).string = "女生做任务";

        }
        this.zxhPanel.getChildByName("des").getComponent(cc.Label).string = desStr;

    }

    onclickZxhPanelCompleteBtn(){
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        if(this.currWhoPlary==0){
            this.currWhoPlary=1;
        }
        else{
            this.currWhoPlary=0;
        }

        this.zxhPanel.active = false;
    }

    openWinPanel() {
        console.log("游戏胜利")
        this.winPanel.active = true;

        if (this.currWhoPlary == 0) {
            this.winPanel.getChildByName("titleLabel").getComponent(cc.Label).string = "女生做任务";
            this.winPanel.getChildByName("winLabel").getComponent(cc.Label).string = "男生胜出";

        }
        else if (this.currWhoPlary == 1) {
            this.winPanel.getChildByName("titleLabel").getComponent(cc.Label).string = "男生做任务";
            this.winPanel.getChildByName("wintitleLabel").getComponent(cc.Label).string = "女生胜出";
        }

    }

    onclickWinPanelCompleteBtn(){
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        this.winPanel.active=false;
        this.gameReStart();
    }

    //游戏重新开始
    gameReStart(){
        this.currAxeIndex_boy = 0;
        this.finallyIndex_boy = 0;
    
        this.currAxeIndex_girl = 0;
        this.finallyIndex_girl = 0;
    
        this.currWhoPlary=0;//0 boy,2 girl

        this.player0.setPosition(288,503);
        this.player1.setPosition(317,503);
    }


    onclickPauseBtn() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        this.node.getChildByName("pauseMinPanel").active = true;

        if (DataManager.instance.isMusicOn) {
            this.musicSp.spriteFrame=this.musicSf[0];
        }
        else {
            this.musicSp.spriteFrame=this.musicSf[1];
        }

        if (DataManager.instance.isSoundOn) {
            this.soundSp.spriteFrame=this.soundSf[0];
        }
        else {
            this.soundSp.spriteFrame=this.soundSf[1];
        }
    }

    onclickPausePanelCloseBtn() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        this.node.getChildByName("pauseMinPanel").active = false;
    }
    onclickPausePanelHomeBtn() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        this.node.getChildByName("pauseMinPanel").active = false;
        this.node.active = false;
    }
    onclickPausePanelMusicBtn() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        if (DataManager.instance.isMusicOn) {
            DataManager.instance.isMusicOn = false;
            this.musicSp.spriteFrame=this.musicSf[1];
            AudioManager.instance.stopMusic();
        }
        else {
            DataManager.instance.isMusicOn = true;
            this.musicSp.spriteFrame=this.musicSf[0];
            AudioManager.instance.playMusic();

        }
    }
    onclickPausePanelSoundBtn() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        if (DataManager.instance.isSoundOn) {
            DataManager.instance.isSoundOn = false;
            this.soundSp.spriteFrame=this.soundSf[1];
        }
        else {
            DataManager.instance.isSoundOn = true;
            this.soundSp.spriteFrame=this.soundSf[0];
        }
    }



}
